const puppeteer =  require("puppeteer");
const cheerio = require("cheerio");
var search = async ctx =>{
    var {keyword} = ctx.request.query;
    var url = `https://music.163.com/#/search/m/?s=${keyword}&type=10`
    console.log(keyword);
    const browser = await puppeteer.launch({
        // headless:false,
        defaultViewport:null
    });
    const page = await browser.newPage();
    await page.goto(url);
    // await page.waitForTimeout(3000)
    // 找到iframe
    var iframe = await page.$("#g_iframe");
    // contentFrame获取iframe中的html
    var frame = await iframe.contentFrame();
    // 抓取
    var content = await frame.content(frame);
    const $ = cheerio.load(content,{
        decodeEntities:false
    })
    let arr=[];
    $(".m-cvrlst li").each((index,value)=>{
        let searchImg = $(value).find(".u-cover-alb2 img").attr("src");
        let searchTitle = $(value).find(".u-cover-alb2 .msk").attr("title")
        let searchName = $(value).find(".f-thide a").html();
        arr.push({
            searchImg,
            searchTitle,
            searchName
        })
    })
    ctx.body = arr;
}
module.exports =search;